package cn.felord.algorithms.sort;

import java.util.Arrays;

/**
 * 插入排序
 *
 * @author Dax
 * @since 10:41  2018/7/15
 */
public class InsertSort {

    private static void swap(int[] arr, int index, int indexTemp) {
        arr[index] = arr[index] + arr[indexTemp];
        arr[indexTemp] = arr[index] - arr[indexTemp];
        arr[index] = arr[index] - arr[indexTemp];
    }


    public static int []  sort(int[] arr){

        for (int i = 1; i <arr.length ; i++) {
            int j=i;
            while (j>0&&arr[j]<arr[j-1]){
                swap(arr,j,j-1);
               j--;
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        int [] arr={1,5,23,0,6,3,3,8,7,4,1,2,4,5};
        System.out.println(Arrays.toString(sort(arr)));
    }
}
